﻿using System;
using System.Collections.Generic;
using HiTeachers.Data.Manager;
using HiTeachers.Entities;
using HiTeachers.Interfaces;
using HiTeachers.Utilities;
using NHibernate;

namespace HiTeachers.Data
{
    public class StudentRequestDao : AbstractNHibernateDao<StudentRequest, Guid>, IStudentRequestDao
    {
        public IList<StudentRequest> GetRelationStdRequests(Guid itId, DateTime aroundDate, int maxItem)
        {
            try
            {
                const string hsql = "from StudentRequest stdrq where stdrq.ID != :itemId and stdrq.IsVerified = 1 and stdrq.IsChecked = 1";

                IQuery query = NHibernateSession.CreateQuery(hsql);

                query.SetGuid("itemId", itId);

                query.SetMaxResults(maxItem);
                query.SetFirstResult(0);

                return query.List<StudentRequest>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return new List<StudentRequest>();
            }
        }

        public int TotalVerifiedRequest()
        {
            try
            {
                const string hsql = "select count(*) from StudentRequest strq where strq.IsVerified = 1 and strq.IsChecked = 1";

                var query = NHibernateSession.CreateQuery(hsql);
                return Convert.ToInt32(query.UniqueResult());
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return 0;
            }
        }

        public IList<StudentRequest> GetVerifiedRequest(int maxResult, int startRowIndex)
        {
            try
            {
                const string hsql = "from StudentRequest strq where strq.IsVerified = 1 and strq.IsChecked = 1 order by strq.PostedDate desc";

                var query = NHibernateSession.CreateQuery(hsql);

                query.SetMaxResults(maxResult);
                query.SetFirstResult(startRowIndex);

                return query.List<StudentRequest>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return new List<StudentRequest>();
            }
        }


        public int TotalNumberOfStudentRequest(int reviewStatus)
        {
            try
            {
                var hsql = "select count(*) from StudentRequest strq";

                if (reviewStatus != EntityType.AllRequests) hsql += " where strq.IsChecked = :reviewStatus";

                var query = NHibernateSession.CreateQuery(hsql);
                if (reviewStatus != EntityType.AllRequests) query.SetBoolean("reviewStatus", (reviewStatus == EntityType.Reviewed));

                return Convert.ToInt32(query.UniqueResult());
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return 0;
            }
        }

        public IList<StudentRequest> GetStudentRequests(int reviewStatus)
        {
            try
            {
                var hsql = "from StudentRequest strq";
                if (reviewStatus != EntityType.AllRequests) hsql += " where strq.IsChecked = :reviewStatus";
                hsql += " order by strq.PostedDate desc";

                var query = NHibernateSession.CreateQuery(hsql);
                if (reviewStatus != EntityType.AllRequests) query.SetBoolean("reviewStatus", (reviewStatus == EntityType.Reviewed));


                return query.List<StudentRequest>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return new List<StudentRequest>();
            }
        }

        public IList<StudentRequest> GetStudentRequests(int reviewStatus, int maximumRows, int startRowIndex)
        {
            try
            {
                var hsql = "from StudentRequest strq";
                if (reviewStatus != EntityType.AllRequests) hsql += " where strq.IsChecked = :reviewStatus";
                hsql += " order by strq.PostedDate desc";

                var query = NHibernateSession.CreateQuery(hsql);
                if (reviewStatus != EntityType.AllRequests) query.SetBoolean("reviewStatus", (reviewStatus == EntityType.Reviewed));

                query.SetMaxResults(maximumRows);
                query.SetFirstResult(startRowIndex);

                return query.List<StudentRequest>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return new List<StudentRequest>();
            }
        }
    }
}
